package com.brayan.webapp.model;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

/**
 * This class represents a thread. A thread could be used to trace publications
 * which are related. A thread does not necessarily require a name. This way
 * publications may be loosely coupled.
 * 
 * @author Brayan
 * 
 */
@Entity
public class PubThread implements Serializable {
	private static final long serialVersionUID = -7271922017926181308L;

	public PubThread() {
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	/** The name is optional */
	private String name;

	@Column
	private Calendar dateCreated;
	@Column
	private Calendar dateModified;

	@ManyToMany(mappedBy = "pubThreads")
	private List<Pub> pups;

	/***** EVENT HOOKS *****/
	@PrePersist
	protected void onCreate() {
		dateCreated = Calendar.getInstance();
	}

	@PreUpdate
	protected void onUpdate() {
		dateModified = Calendar.getInstance();
	}

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Calendar getDateCreated() {
		return dateCreated;
	}

	public Calendar getDateModified() {
		return dateModified;
	}

	public List<Pub> getPubs() {
		return pups;
	}

	public void setPubs(List<Pub> pups) {
		this.pups = pups;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (!(obj instanceof PubThread))
			return false;
		PubThread objectToCheck = (PubThread) obj;
		boolean isShallowCopy = (this.id.equals(objectToCheck.id));
		return isShallowCopy;
	}

	@Override
	public int hashCode() {
		return (this.id.hashCode());
	}
}
